package com.yiwei.ydd.activity;

import android.content.ClipData;
import android.content.ClipboardManager;
import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.yiwei.ydd.R;
import com.yiwei.ydd.api.Api;
import com.yiwei.ydd.api.model.AboutUsModel;
import com.yiwei.ydd.base.BaseActivity;
import com.yiwei.ydd.util.Network;
import com.yiwei.ydd.util.RxLifeUtil;
import com.yiwei.ydd.util.ToastUtil;
import com.yiwei.ydd.util.UI;
import com.yiwei.ydd.util.Util;
import com.yiwei.ydd.view.V19FrameLayout;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.OnLongClick;
import butterknife.Unbinder;

/**
 * Created by xsooy-pc on 17-12-8.
 */

public class AboutUsActivity extends BaseActivity {
    @BindView(R.id.btn_back)
    ImageView btnBack;
    @BindView(R.id.txt_title)
    TextView txtTitle;
    @BindView(R.id.txt_right)
    TextView txtRight;
    @BindView(R.id.layout_title)
    V19FrameLayout layoutTitle;
    @BindView(R.id.txt_version)
    TextView txtVersion;
    @BindView(R.id.txt_content)
    TextView txtContent;
    @BindView(R.id.txt_wechat)
    TextView txtWechat;
    @BindView(R.id.txt_web)
    TextView txtWeb;
    @BindView(R.id.txt_phone)
    TextView txtPhone;
    @BindView(R.id.txt_email)
    TextView txtEmail;

    private Unbinder unbiner;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_about_us);
        initBar();
        unbiner = ButterKnife.bind(this);
        init();
    }

    private void init() {
        txtTitle.setText("关于我们");
        getAboutUs();
    }

    public void getAboutUs() {
        if (!Network.checkNetwork(this)) {
            ToastUtil.show(this, "网络连接失败");
            return;
        }


        loadingDialog.show();
        Api.api_service.getAboutUs().compose(RxLifeUtil.checkOn(this)).
                doFinally(() -> {
                    try {
                        loadingDialog.dismiss();
                    } catch (NullPointerException e) {

                    }
                }).
                subscribe(result -> {
                    AboutUsModel.DatasBean item = result.datas;
                    txtContent.setText(item.desc);
                    txtPhone.setText(item.telphone);
                    txtVersion.setText("v" + Util.getVersion(this) + "版本");
                    txtWechat.setText(item.gf_wechat_no);
                    txtWeb.setText(item.gf_website);
                    txtEmail.setText(item.email);
                });
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        unbiner.unbind();
        unbiner = null;
    }

    @OnClick(R.id.btn_back)
    public void onViewClicked() {
        finish();
    }

    @OnLongClick({R.id.txt_content, R.id.txt_wechat, R.id.txt_web, R.id.txt_phone, R.id.txt_email})
    boolean onLongClick(View view) {
        ClipboardManager cm = (ClipboardManager) getSystemService(Context.CLIPBOARD_SERVICE);
// 创建普通字符型ClipData

        String copyText = "";
        switch (view.getId()) {
            case R.id.txt_content:
                copyText = UI.toString(txtContent);
                break;
            case R.id.txt_wechat:
                copyText = UI.toString(txtWechat);
                break;
            case R.id.txt_web:
                copyText = UI.toString(txtWeb);
                break;
            case R.id.txt_phone:
                copyText = UI.toString(txtPhone);
                break;
            case R.id.txt_email:
                copyText = UI.toString(txtEmail);
                break;
            default:
                break;
        }

        ClipData mClipData = ClipData.newPlainText("Label", copyText);
// 将ClipData内容放到系统剪贴板里。
        cm.setPrimaryClip(mClipData);
        ToastUtil.makeText(this, "复制成功");
        return false;
    }
}
